package com.example.demo.tools;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description: 验证当前是否登录，来判断是否进行拦截
 * User: su
 * Date: 2023-02-19
 * Time: 11:10
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // session 得到用户信息
        // 得到 userinfo 返回 true 已登录
        // 未得到 userinfo 返回 false 未登陆
        HttpSession session = request.getSession(false);
        if (session != null &&
                session.getAttribute(Constant.SESSION_KEY_USERINFO) != null) {
            return true;
        }

        // 未登录
        response.sendRedirect("/adminLogin.html");

        return false;
    }
}
